//
//  LXEmoticonKeyboardView.swift
//  weibo
//
//  Created by lixin on 16/4/23.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

class LXEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
    private func setUI(){
        
        addSubview(emoticonToolbar)
        
        addSubview(emoticonPageView)
        
        addSubview(pageControl)
       
        emoticonToolbar.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.bottom.equalTo(self)
            
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(self)
            
            make.bottom.equalTo(emoticonToolbar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            
            make.bottom.equalTo(emoticonPageView)
        }
        
        
        emoticonToolbar.LXEmoticonToolbarClosure = {[weak self] (type: LXEmoticonToolbarType) in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            self?.pageControl.numberOfPages = LXEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
          
            self?.pageControl.currentPage = 0
        }
    }
    
    
    
    // MARK: -  懒加载控件
    private lazy var emoticonToolbar: LXEmoticonToolbar = LXEmoticonToolbar()
    
    private lazy var emoticonPageView: LXEmoticonPageView = {
        
        let view = LXEmoticonPageView()
        
        view.bounces = false
       
        view.pagingEnabled = true
    
        view.delegate = self
       
        view.showsHorizontalScrollIndicator = false
        
        view.showsVerticalScrollIndicator = false
        
        return view
    }()

    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        pageControl.numberOfPages = LXEmoticonTools.sharedTools.allEmoticons[0].count
       
        pageControl.currentPage = 0
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
        return pageControl
    }()
    
}

//MARK: - 代理方法
extension LXEmoticonKeyboardView: UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let centerX = SCREENW / 2  + scrollView.contentOffset.x
      
        let cells =  emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if cells.count == 2{
            
            var indexPath: NSIndexPath?
            
            let firstCell = cells.first!
            
            let lastCell = cells.last!
            
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
                
            }else{
                
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            
            emoticonToolbar.setEmoticonCurrentButton(indexPath!.section)
           
            pageControl.numberOfPages = LXEmoticonTools.sharedTools.allEmoticons[indexPath!.section].count
            
            pageControl.currentPage = indexPath!.item
        }

    }
}

